package com.zero.controller;

import com.zero.entity.FamilyMember;
import com.zero.mapper.MenuMapper;
import com.zero.service.LoginService;
import com.zero.service.MenuService;
import com.zero.util.Loginer;
import com.zero.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private MenuService menuService;

    @PostMapping("/login")
    public Result login(@RequestBody Loginer loginer){
        return loginService.login(loginer);
    }

    /*
    [ Menu(id=1, title=基本资料, type=0, parentId=0, href=null, icon=null, target=null,child=null),
      Menu(id=2, title=家庭管理, type=0, parentId=1, href=admin/family.html, icon=null, target=null, child=null),
      Menu(id=3, title=用户管理, type=0, parentId=1, href=admin/user.html, icon=null, target=null, child=null),
      Menu(id=4, title=网站信息, type=0, parentId=0, href=admin/web.html, icon=null, target=null, child=null),
      Menu(id=5, title=修改密码, type=0, parentId=0, href=admin/update_pd.html, icon=null, target=null, child=null)]

     */
    @GetMapping("/menu")
    public Result getMenus(HttpServletRequest request){
        if (request.getAttribute("admin")!=null){
            return menuService.getAdminMenu();
        }
        if(request.getAttribute("familyMember") != null){
            FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
            return menuService.getFamilyMemberMenu(familyMember.getId());
        }
        return Result.fail("菜单接口错误");
    }

    @PostMapping("/register")
    public Result register(@RequestBody FamilyMember familyMember){
        return loginService.register(familyMember);
    }
}
